﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace SlamCms.SharePointServices
{
	public static class SlamService
	{
		private static string _slamServiceUrl = "/_vti_bin/Slam.svc";
		public static string GetNewSlamId(string siteUrl, string listName)
		{
			return GetNewSlamId(siteUrl, listName, "");
		}

		public static string GetNewSlamId(string siteUrl, string listName, string contentTypeName)
		{
			string requestUrl = siteUrl.TrimEnd('/') + _slamServiceUrl + "/GetNewSlamId?list=" + listName;
			if (!String.IsNullOrEmpty(contentTypeName))
				requestUrl += "&contentType=" + contentTypeName;

			HttpWebRequest idRequest = ServiceClients.CreateAuthenticatedRequest(requestUrl);

			WebResponse response = idRequest.GetResponse();
			string id = "";
			if (response.ContentLength > 0)
			{
				using (StreamReader reader = new StreamReader(response.GetResponseStream()))
				{
					id = reader.ReadToEnd().TrimStart('"').TrimEnd('"');
				}
			}

			return id;
		}
	}
}
